package com.googlecode.microanalyst.palette.edit;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import com.googlecode.microanalyst.palette.figure.LabeledConnectionFigure;
import com.googlecode.microanalyst.palette.model.LabeledConnection;
import com.googlecode.microanalyst.palette.model.cmd.LabeledConnectionLabelCommand;

public class LabeledConnectionDirectEditPolicy extends DirectEditPolicy {

	/**
	 * @see DirectEditPolicy#getDirectEditCommand(DirectEditRequest)
	 */
	protected Command getDirectEditCommand(DirectEditRequest edit) {
		String labelText = (String) edit.getCellEditor().getValue();
		final LabeledConnectionEditor label = (LabeledConnectionEditor) getHost();
		final LabeledConnection model = (LabeledConnection) label.getModel();
		LabeledConnectionLabelCommand command = new LabeledConnectionLabelCommand(
				model, labelText);
		return command;
	}

	/**
	 * @see DirectEditPolicy#showCurrentEditValue(DirectEditRequest)
	 */
	protected void showCurrentEditValue(DirectEditRequest request) {
		String value = (String) request.getCellEditor().getValue();
		((LabeledConnectionFigure) getHostFigure()).setText(value);
		// hack to prevent async layout from placing the cell editor twice.
		getHostFigure().getUpdateManager().performUpdate();

	}

}
